#include "ProductTestSpk.h"

#define DEF_TAG             "TestSpk"

#if CMAKE_INGENIC_T41N
#define TUTK_SPK_BUFFER_SIZE     160
#else
#define TUTK_SPK_BUFFER_SIZE     1280
#endif

extern Com_u8 g_ProductTest_Media_Running;
extern Com_s32 g_ProductTest_ThreadNum;
pthread_mutex_t g_ProductTest_Spk_WavPlay_Mutex;

Com_pvoid ProductTest_Spk_WavPlay_Thread(Com_pvoid arg)
{
    pthread_mutex_lock(&g_ProductTest_Spk_WavPlay_Mutex);

    Com_s32 size = 0;
    Com_s32 ret = -1;
    Com_u8 cnt = 0;
    Com_u8 buf[TUTK_SPK_BUFFER_SIZE] = {0};
    FILE *play_file;
    pStProductTestSpkInfo info = (pStProductTestSpkInfo)arg;

    memset(buf,0,sizeof(buf));

    play_file = fopen(info->path, "rb");
    if(play_file == NULL)
    {
        PRT_ERR(DEF_TAG,"fopen %s failed\n",info->path);
        goto exit;
    }

    fseek(play_file, 44, SEEK_SET);
    while(DEF_COM_TRUE)
    {
        if(cnt >= info->cnt)
        {
            break;
        }

        size = fread(buf,1,TUTK_SPK_BUFFER_SIZE,play_file);
        if(size > 0)
        {
            ret = Ext_SdkSpk_Send_Buff(buf,size);
            if(ret != DEF_COM_SUCCESS)
            {
                break;
            }
        }
        else if (size == 0)
        {
            ++cnt;
            fseek(play_file, 44, SEEK_SET);
            usleep(100 * 1000);
            continue;
        }
    }

    Ext_SdkSpk_Send_End_Buff();
    fclose(play_file);
exit:

    if(info->path != NULL)
    {
        free(info->path);
    }

    if(info != NULL)
    {
        free(info);
    }

    pthread_mutex_unlock(&g_ProductTest_Spk_WavPlay_Mutex);
    pthread_exit(0);
}

void *Ext_ProductTest_Spk_Thread(void* arg)
{
    Com_s32 ret;
    Com_s8 buf[1280];
    pStSdkRingBufInfoProducer pro;
    pStSdkRingBufInfo ring_buff;
    StProductTestSpkInfo info;

    pro = (pStSdkRingBufInfoProducer)arg;
    ring_buff = &pro->spk_ring_buffer;

    StSdkRingBufFrame frame;
    frame.frame_buf = buf;

    ret = Ext_SdkSpk_Init();
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"spk init failed !\n");
        return NULL;
    }

    pthread_mutex_init(&g_ProductTest_Spk_WavPlay_Mutex, NULL);
    PRT_NORMAL("start spk !!\n");

    Ext_SdkGpioWrite_Spk(DEF_COM_LEVEL_LOW);

    info.cnt = 3;
    info.path = DEF_WAV_DING_DONG;
    Ext_ProductTest_Spk_WavPlay_Init(&info);

    g_ProductTest_ThreadNum++;

    while(g_ProductTest_Media_Running)
    {
        ret = pro->get_one_frame_from_ring_buffer(ring_buff,&frame);
        if(ret < 0)
        {
            usleep(20 * 1000);
            continue;
        }

        Ext_SdkSpk_Play((Com_pu8)frame.frame_buf,frame.frame_len);
    }

    Ext_SdkGpioWrite_Spk(DEF_COM_LEVEL_HIGHT);

    ret = Ext_SdkSpk_Exit();
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"spk exit failed !\n");
        return NULL;
    }

    pthread_mutex_destroy(&g_ProductTest_Spk_WavPlay_Mutex);
    g_ProductTest_ThreadNum--;
    pthread_exit(0);
}

void Ext_ProductTest_Spk_WavPlay_Init(pStProductTestSpkInfo info)
{
    pthread_t id;
    Com_ps8 wav_path;
    Com_s32 cnt = info->cnt;
    pStProductTestSpkInfo s_info = info;

    wav_path = (char *)malloc(64);
    memset(wav_path,0,64);
    memcpy(wav_path,info->path,64);
    s_info = (pStProductTestSpkInfo)malloc(sizeof(StProductTestSpkInfo));
    s_info->path = wav_path;
    s_info->cnt = cnt;
    PRT_NORMAL("wav: %s,cnt: %d\n",s_info->path,s_info->cnt);

    pthread_create(&id, NULL, ProductTest_Spk_WavPlay_Thread,(Com_pvoid)s_info);
    pthread_detach(id);
}
